package com.zeroup.auto.assists.step.dy;

import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.ven.assists.stepper.StepData;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.assists.next.StepBeanData;
import com.zeroup.auto.utils.AssistsUtils;
import com.ven.assists.Assists;
import com.ven.assists.stepper.StepCollector;

import java.util.List;

import kotlin.Unit;

public class TopSearchStep extends DouYinBaseStep {

    public static String SEARCH_ID ="com.ss.android.ugc.aweme:id/hj8";
    public static String INPUT_ID ="com.ss.android.ugc.aweme:id/et_search_kw";

    public static String SEARCH_BTN = "com.ss.android.ugc.aweme:id/crc";



    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        stepCollector.next(1,false,(i,c)->{
            List<AccessibilityNodeInfo> infoList = Assists.INSTANCE.findById(SEARCH_ID);
            if(infoList.isEmpty()){
                return Unit.INSTANCE;
            }
            for (AccessibilityNodeInfo info:infoList) {
                if(info.isClickable()){
                    info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    break;
                }
            }
            return AssistsUtils.nextStep(i.getData());
        }).next(2,false,(i,c)->{
            List<AccessibilityNodeInfo> infoList = Assists.INSTANCE.findById(INPUT_ID);
            if(infoList.isEmpty()){
                return Unit.INSTANCE;
            }
            Object serviceData = AssistsUtils.getServiceData(i.getData());
            if(serviceData==null){
                return Unit.INSTANCE;
            }
            StepBeanData input_txt = (StepBeanData)serviceData;
            for (AccessibilityNodeInfo info:infoList) {
                Assists.INSTANCE.paste(info,input_txt.getDataValue());
            }
            return AssistsUtils.nextStep(i.getData());
        }).next(3,false,(i,c)->{
            AssistsUtils.getServiceData(i.getData());
            List<AccessibilityNodeInfo> infoList = Assists.INSTANCE.findById(SEARCH_BTN);
            if(infoList.isEmpty()){
                return Unit.INSTANCE;
            }
            infoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            return AssistsUtils.nextStep(i.getData());
        });
    }
}
